package edu.stanford.bmir.protege.web.client.ui.ontology.transform;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;

import edu.stanford.bmir.protege.web.shared.transform.MappingBean;

public class StepThree extends Composite {
	

	@UiField
	DataGrid<MappingBean> table;
	
	@UiField
	Button startImport;
	
	@UiField
	Button returnPre;
	
	List<MappingBean> mblst = new ArrayList<MappingBean>(); 
	
	
	private static StepThreeUiBinder uiBinder = GWT
			.create(StepThreeUiBinder.class);

	interface StepThreeUiBinder extends UiBinder<Widget, StepThree> {
	}
	
	MainView view;

	public StepThree( final MainView view ) {
		initWidget(uiBinder.createAndBindUi(this));
		this.view = view;
		
		mblst = view.getMblst();
		
		addGrid();
		
		returnPre.addClickHandler( new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				
				view.openStepTwo();
				
			}
			
		});
		
		
		startImport.addClickHandler( new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				
				view.createOWL();
				
			}
			
		});
		
		 
	} 
	private void addGrid () {
		
		table.setWidth( "600px" );
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		

		TextColumn<MappingBean> houseNumber = new TextColumn<MappingBean>() {
			@Override
			public String getValue(MappingBean object) {
				return object.getName();
			}
		};
		table.addColumn(houseNumber, "属性");
		
		TextColumn<MappingBean> country = new TextColumn<MappingBean>() {
			@Override
			public String getValue(MappingBean object) {
				return object.getMapping();
			}
		};
		table.addColumn(country, "本体映射");
		
/*		SelectMappingColumn smc = new SelectMappingColumn();
		
		table.addColumn(smc);*/

		table.setRowCount(mblst.size(), true);
		table.setRowData(0, mblst);
        
	}
	
	private class SelectMappingColumn extends Column<MappingBean, String> {

        private SelectMappingColumn() {
            super(new ClickableTextCell() {

            });
            setSortable(true);
        }

        @Override
        public String getValue(MappingBean object) {
            return object.getName();
        }

        @Override
        public void render(Cell.Context context, MappingBean object, SafeHtmlBuilder sb) {
            sb.appendHtmlConstant("<div>设置映射</div>");
        }

        @Override
        public VerticalAlignmentConstant getVerticalAlignment() {
            return HasVerticalAlignment.ALIGN_TOP;
        }

        @Override
        public void onBrowserEvent(Cell.Context context, Element elem, MappingBean object, NativeEvent event) {
            super.onBrowserEvent(context, elem, object, event);
            //mapping.showWindow();
            
        }
    }

}